﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using DelonixRegiaHotel.Classes;
using DelonixRegiaHotel.DBManager;
using System.Collections;

namespace DelonixRegiaHotel
{
    public partial class GetStaff : Form
    {

        public GetStaff()
        {
            InitializeComponent();

            //display duty info in combo box
            foreach (Duty d in StaffAndDutyDBManager.GetAllDutyInfo())
            {
                cbDutyTypeID.Items.Add(d.DutyName + " (" + d.DutyTypeID + ")");
            }

            //display staff's firstname, lastname & nric in combo box for search
            foreach (Staff s in StaffAndDutyDBManager.GetAllStaffInfo())
            {
                comboStaffSearch.Items.Add(s.FirstName + " " + s.LastName + " (Nric: " + s.Nric + ")");
            }
        }

       

        private void comboStaffSearch_SelectedIndexChanged(object sender, EventArgs e)
        {
             ArrayList aStaffInfo = StaffAndDutyDBManager.GetAllStaffInfo();
             ArrayList aDutyInfo = StaffAndDutyDBManager.GetAllDutyInfo();
             foreach (Staff s in aStaffInfo)
             {
                 foreach (Duty d in aDutyInfo)
                 {
                
                     if(comboStaffSearch.SelectedItem.ToString().Contains(s.FirstName) && comboStaffSearch.SelectedItem.ToString().Contains(s.FirstName) && comboStaffSearch.SelectedItem.ToString().Contains(s.Nric))
                 {
                     tbxStaffId.Text = s.StaffID;
                     tbxfn.Text = s.FirstName;
                     tbxln.Text = s.LastName;
                     tbxNric.Text = s.Nric;
                     tbxAddress.Text = s.HomeAddress;
                     tbxPostalCode.Text = Convert.ToString(s.PostalCode);
                     maskedTextBoxDob.Text = Convert.ToString(s.Dob.ToShortDateString());
                     tbxContactNo.Text = Convert.ToString(s.ContactNo);
                     tbxEmail.Text = s.Email;
                     tbxBankAccNo.Text = Convert.ToString(s.BankAccountNo);
                     tbxPassword.Text = s.Password;
                     lblCurrAccessLvl.Text = Convert.ToString(s.AccessLevel);
                     lblCurrDutyTypeId.Text = s.DutyTypeID; 
                     lblCurrShiftType.Text = s.ShiftType;
                     lblCurrHourlyRate.Text = Convert.ToString(s.HourlyRate);
                     tbxWorkHours.Text = Convert.ToString(s.WorkHours);
                     tbxCurrShiftDays.Text = s.ShiftDay;
                 }



                 }
             }

    }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            if (tbxAddress.Text.Equals("") || tbxPostalCode.Text.Equals("") || tbxContactNo.Text.Equals("") || tbxEmail.Text.Equals("") || tbxBankAccNo.Text.Equals("") || tbxPassword.Text.Equals("") || tbxHourlyRate.Text.Equals("") || cbAccessLvl.SelectedIndex == -1 || cbDutyTypeID.SelectedIndex == -1 || cbShiftType.SelectedIndex == -1)
            {
                MessageBox.Show("Ensure that all fields are filled and options are selected");
                return;
            }

            if (tbxContactNo.Text.Length != 8)
            {
                MessageBox.Show("Invalid contact number");
                return;
            }

            ArrayList aDutyTypeId = new ArrayList();

            // Declare dutyinfo in the arraylist
            aDutyTypeId = StaffAndDutyDBManager.GetAllDutyInfo();
            Staff staff = new Staff();
            foreach (Duty d in aDutyTypeId)
            {
                // if selected item in combo box contain the duty type id
                if (cbDutyTypeID.SelectedItem.ToString().Contains(d.DutyTypeID))
                {
                    staff.DutyTypeID = d.DutyTypeID;
                }
            }

            //check selected shiftday
            string temp = "";
            if (clbShiftDays.SelectedItem == null)
            {
                MessageBox.Show("Please select shift day(s) for Staff");
                return;
            }
            else
            {
                foreach (Object checkeditem in clbShiftDays.CheckedItems)
                {
                    temp += checkeditem + ", ";
                }
            }
            temp = temp.Remove(temp.Length - 2);

            staff.ShiftDay = temp;
            staff.StaffID = tbxStaffId.Text;
            staff.HomeAddress = tbxAddress.Text;
            staff.PostalCode = Convert.ToInt32(tbxPostalCode.Text);
            staff.Dob = Convert.ToDateTime(maskedTextBoxDob.Text);
            staff.ContactNo = Convert.ToInt32(tbxContactNo.Text);
            staff.Email = tbxEmail.Text;
            staff.BankAccountNo = Convert.ToInt32(tbxBankAccNo.Text);
            staff.Password = tbxPassword.Text;
            staff.AccessLevel = Convert.ToInt32(cbAccessLvl.SelectedItem);
            staff.ShiftType= Convert.ToString(cbShiftType.SelectedItem);
            staff.HourlyRate = Convert.ToDouble(tbxHourlyRate.Text);
            staff.ShiftType = Convert.ToString(cbShiftType.SelectedItem);

            if (StaffAndDutyDBManager.UpdateStaffInfoByID(staff) == 1)
            {
                MessageBox.Show("Update is successful");
            }
            else
            {
                MessageBox.Show("Update is unsuccessful");
            }
            this.Hide();
        }

        private void tbxPostalCode_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxPostalCode.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }

        //validate textboxes which only accept integers
        private void tbxContactNo_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxContactNo.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }

        private void tbxBankAccNo_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxBankAccNo.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }

        private void tbxHourlyRate_Validating(object sender, CancelEventArgs e)
        {
            try
            {
                int intEntered = int.Parse(tbxHourlyRate.Text);
            }

            catch
            {
                e.Cancel = true;
                MessageBox.Show("Invalid entry");
            }
        }

    }
}